(function($){
	var KEY = "jforest.playMP3";
	
	var getFlashObject = function(movieName) {
		if (window.document[movieName]) {
			return window.document[movieName];
		}   
		if (navigator.appName.indexOf("Microsoft Internet")==-1) {
			if (document.embeds && document.embeds[movieName]){
				return document.embeds[movieName];
			}else {
				return document.getElementById(movieName);
			}
		}   
	};
	
	var callback = function(url){
		var asound = getFlashObject("MP3player");
		if(asound){
			asound.SetVariable("f",url);
			asound.GotoFrame(1);
		}   
		return false;
	};
	
	var PlayMP3 = function(element,options){
		this.$element = $(element);
		this.options = $.extend({
			webRoot:''
		},options);
	
		var asound = getFlashObject("MP3player");
		if(!asound){
			var swfplayer = ' <object style="height:0px;width:0px;overflow:hidden;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="MP3player" align="absmiddle">';
			swfplayer += '        <param name="allowScriptAccess" value="always" />';
			swfplayer += '        <param name="movie" value="'+this.options.webRoot+'/jforest/swf/asound.swf" />';
			swfplayer += '        <param name="quality" value="high" />';
			swfplayer += '        <embed src="'+this.options.webRoot+'/jforest/swf/asound.swf" quality="high" width="0" height="0" name="MP3player" align="absmiddle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			swfplayer += '    </object>';
			this.$element.after(swfplayer);
		}
		var timer = null;
		var mp3Url = this.$element.attr( 'mp3' );
		
		this.$element.click(function(){
			return callback(mp3Url);
		});
		this.$element.mouseover(function(){
			clearTimeout(timer);
			timer = setTimeout(function(){
				callback(mp3Url);
			}, 100);
		});
		this.$element.mouseout(function(){
			clearTimeout(timer);
		});
	};

	PlayMP3.prototype = {};
	
	$.fn.jfPlayMP3 = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				$this.data(KEY,new PlayMP3(this,opts)); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
                if(entity === undefined){return;}
                value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfPlayMP3.Constructor = PlayMP3;
	
})(window.jQuery)